package ph.com.gs3.formalistics.model.facades;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import org.json.JSONException;
import org.json.JSONObject;

import ph.com.gs3.formalistics.model.ldao.DocumentsLDAO;
import ph.com.gs3.formalistics.model.ldao.UserDocumentsLDAO;
import ph.com.gs3.formalistics.model.vo.application.SearchCondition;
import ph.com.gs3.formalistics.model.vo.business.document.Document;
import ph.com.gs3.formalistics.model.vo.business.form.Form;
import android.content.Context;

public class DocumentsDataReader {

	public static final String TAG = DocumentsDataReader.class.getSimpleName();

	private UserDocumentsLDAO userDocumentsLDAO;
	private DocumentsLDAO documentsLDAO;

	public DocumentsDataReader(Context context) {
		userDocumentsLDAO = new UserDocumentsLDAO(context);
		documentsLDAO = new DocumentsLDAO(context);
	}

	public String[] getFormDocumentIdArray(int formId) {
		List<String> documents = getFormDocumentIdList(formId);
		return documents.toArray(new String[documents.size()]);
	}

	public int getUserDocumentCount(int userId) {

		int userDocumentCount = 0;
		try {
			userDocumentsLDAO.open();
			userDocumentCount = userDocumentsLDAO.getUserDocumentsCount(userId);
		} finally {
			userDocumentsLDAO.close();
		}

		return userDocumentCount;

	}

	public List<String> getFormDocumentIdList(int formId) {

		List<String> documents = new ArrayList<>();

		try {
			documentsLDAO.open();
			documents = documentsLDAO.getFormDocumentIdList(formId);
		} finally {
			documentsLDAO.close();
		}

		return documents;

	}

	public Document getDocument(int id) {

		Document document = null;

		try {
			documentsLDAO.open();
			document = documentsLDAO.getDocument(id);
		} catch (JSONException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			documentsLDAO.close();
		}

		return document;

	}

	public List<JSONObject> search(Form form, List<String> resultFieldNames, int ownerId,
	        List<SearchCondition> conditions) {

		List<JSONObject> searchResults = new ArrayList<>();

		try {
			documentsLDAO.open();
			searchResults = documentsLDAO.search(form, resultFieldNames, ownerId, conditions);
		} catch (JSONException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			documentsLDAO.close();
		}

		return searchResults;

	}

	public Map<String, String> getFormDocumentIdAndWebIdPairList(int formId) {

		Map<String, String> documentIdAndWebId = new HashMap<>();

		try {
			documentsLDAO.open();
			documentIdAndWebId = documentsLDAO.getFormDocumentWebIdAndIdPairList(formId);
		} finally {
			documentsLDAO.close();
		}

		return documentIdAndWebId;

	}

	public String[] getDocumentWebIdArrayFromIdAndWebIdPairList(
	        Map<String, String> idAndWebIdPairList) {

		Iterator<String> iterator = idAndWebIdPairList.keySet().iterator();
		String[] documentWebIdArray = new String[idAndWebIdPairList.size()];
		int index = 0;

		while (iterator.hasNext()) {
			String key = iterator.next();
			documentWebIdArray[index] = key;
			// documentWebIdArray[index] = idAndWebIdPairList.get(key);
			index++;
		}

		return documentWebIdArray;

	}

}
